﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class PressureSoreEventListController : BaseController
    {
        // GET: AdverseEvent/PressureSoreEventList
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 压疮事件列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string whereSql = GetWhereSql();
            SelectBuilder data = new SelectBuilder();
            data.Having = "";
            data.GroupBy = "";
            data.OrderBy = "ID DESC";
            data.From = "AdverseEvent";
            data.Select = "ID,EventTypeName,NurseName,AreaName,OccurrTime,CreateTime,Status,(SELECT Value FROM AdverseEventModeRecord(NOLOCK) WHERE EventID = AdverseEvent.ID AND ModeName ='压伤类型') AS YsType";
            data.WhereSql = whereSql;
            data.PagingCurrentPage = pageIndex;
            data.PagingItemsPerPage = pageSize;
            int total = 0;
            DataTable dt = BaseService<Model.AdverseEvent>.GetQueryManyForPage_Proc(data, out total);
            AdverseEventManager.GetYsType(dt);
            var result = new { Total = total, Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            int year = DataConvert.StrToInt(Request["year"]);
            int quarter = DataConvert.StrToInt(Request["quarter"]);
            int month = DataConvert.StrToInt(Request["month"]);
            int eventTypeID = DataConvert.StrToInt(Request["eventTypeID"]);

            string whereSql = "CreateUserID = '" + loginUser.Personnel_id + "' and Flag_Yc = 1";
            if (quarter == 0)
            {
                whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, 1, 1).ToString(), new DateTime(year, 1, 1).AddYears(1).ToString());
            }
            else
            {
                if (month == 0)
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, quarter * 3 - 2, 1).ToString(), new DateTime(year, quarter * 3 - 2, 1).AddMonths(3).ToString());
                }
                else
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, month, 1).ToString(), new DateTime(year, month, 1).AddMonths(1).ToString());
                }
            }

            return whereSql;
        }
    }
}